import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/LoginPage.vue')
    },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/workspace',
      children: [
        {
          path: '/workspace',
          component: () => import('@/views/workspace/WorkspacePage.vue')
        },
        {
          path: '/user',
          component: () => import('@/views/user/UserManage.vue')
        },
        {
          path: '/employee',
          component: () => import('@/views/employee/EmployeeManage.vue')
        },
        {
          path: 'member',
          component: () => import('@/views/member/MemberManage.vue')
        },
        {
          path: '/project/type',
          component: () => import('@/views/project/ProjectTypeManage.vue')
        },
        {
          path: '/project',
          component: () => import('@/views/project/ProjectManage.vue')
        },
        {
          path: '/item/category',
          component: () => import('@/views/item/ItemCategoryManage.vue')
        },
        {
          path: '/item',
          component: () => import('@/views/item/ItemManage.vue')
        },
        {
          path: '/item/stock',
          component: () => import('@/views/item/ItemStockManage.vue')
        },
        {
          path: '/business',
          component: () => import('@/views/business/BusinessManage.vue')
        },
        {
          path: '/car',
          component: () => import('@/views/car/CarManage.vue')
        },
        {
          path: '/announcement',
          component: () => import('@/views/announcement/AnnouncementManage.vue')
        },
        {
          path: '/feedback',
          component: () => import('@/views/feedback/FeedbackManage.vue')
        },
        {
          path: '/report',
          component: () => import('@/views/report/ReportPage.vue')
        }
      ]
    }
  ]
})

// 登录访问拦截
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') {
    ElNotification({
      title: '请先登录',
      type: 'error'
    })
    return '/login'
  }
  if (userStore.token && to.path === '/login') {
    ElNotification({
      title: '已登录',
      type: 'success'
    })
    return '/'
  }
})

export default router
